import CryptoJS from './crypto-js';
let Base64 = CryptoJS.enc.Base64
let Aes = CryptoJS.AES
let Utf8 = CryptoJS.enc.Utf8
let aesCfg = {
  mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7
}

export const encrypt = (data,iv,key) => {
  let cfg = {
    iv: Base64.parse(iv),
    ...aesCfg
  }
  return Aes.encrypt(Utf8.parse(data),Base64.parse(key),cfg).toString()
}


export const base64Encode = (str) => {
  if (str) {
    return Base64.stringify(Utf8.parse(str))
  }
  return ''
}

export const base64Decode = (base64Str) => {
  if (base64Str && /^[A-Za-z0-9\+\/]{2,}={0,2}$/.test(base64Str)) {
    return Base64.parse(base64Str).toString(Utf8)
  }
  return false
}